package org.tigr.htc.cmd;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/**
 * The <b>CmdState</b> class follows the type safe enum pattern to define 
 * different command states.
 * 
 */

public class CmdState {
    /**
     * <code>name</code> holds the string name of state
     */
    private final String name;

    /**
     * Creates a new <code>CmdState</code> instance.
     *
     * @param name a <code>String</code> value
     */
    private CmdState(String name) {
        this.name = name;
    }

    /**
     * <code>toString</code> overridden from Object to print name.
     *
     * @return a <code>String</code> value
     */
    public String toString() {
        return name;
    }

    /**
     * <code>getState</code> takes a type as a string and returns the
     * correct CmdState object. Needed for object marshalling.
     *
     * @param p_state a <code>String</code> value
     * @return a <code>CmdState</code> value
     */
    public static CmdState getState(String p_state) {
        if(p_state != null) {
            if(p_state.equals("WAITING")) {
                return CmdState.WAITING;
            } else if(p_state.equals("INIT")) {
                return CmdState.INIT;
            } else if(p_state.equals("RUNNING")) {
                return CmdState.RUNNING;
            } else if(p_state.equals("FINISHED")) {
                return CmdState.FINISHED;
            } else if(p_state.equals("FAILED")) {
                return CmdState.FAILED;
            } else if (p_state.equals("INTERRUPTED")) {
                return CmdState.INTERRUPTED;
            } else if (p_state.equals("SUSPENDED")) {
                return CmdState.SUSPENDED;
            }
        }
        return CmdState.UNKNOWN;
    }
    
    // state command is in when created
    public static final CmdState INIT = new CmdState("INIT");
    // init -> waiting and submit event is fired
    public static final CmdState WAITING = new CmdState("WAITING");
    // (waiting, suspended) -> running and start event is fired
    public static final CmdState RUNNING = new CmdState("RUNNING");
    // running -> finished and finish event is fired
    public static final CmdState FINISHED = new CmdState("FINISHED");
    // * -> failure and failure event is fired
    public static final CmdState FAILED = new CmdState("FAILED");
    // running -> interrupt and interrupted event is fired
    public static final CmdState INTERRUPTED = new CmdState("INTERRUPTED"); 
    // running -> suspend and suspend event is fired
    public static final CmdState SUSPENDED = new CmdState("SUSPENDED"); 
    // suspend -> running and resume event is fired

    public static final CmdState UNKNOWN = new CmdState("UNKNOWN"); 
    
}
